<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ConcurrentHashMap 深度解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .highlight-box {
            border-left: 4px solid #667eea;
            background-color: rgba(102, 126, 234, 0.05);
        }
        .code-block {
            font-family: 'Courier New', Courier, monospace;
            background-color: #f8f9fa;
            border-radius: 4px;
            padding: 1rem;
            overflow-x: auto;
        }
        .feature-icon {
            width: 48px;
            height: 48px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            background-color: rgba(102, 126, 234, 0.1);
            color: #667eea;
            margin-right: 1rem;
        }
        .section-divider {
            height: 1px;
            background: linear-gradient(to right, transparent, rgba(0,0,0,0.1), transparent);
        }
        .first-letter {
            font-size: 3.5em;
            float: left;
            line-height: 0.8;
            margin-right: 8px;
            font-weight: 700;
            color: #667eea;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <header class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 md:px-12">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4">ConcurrentHashMap</h1>
                    <h2 class="text-xl md:text-2xl font-light mb-6">Java高并发编程的核心组件深度解析</h2>
                    <p class="text-lg mb-8 opacity-90">一个为多线程环境设计的高效线程安全哈希表实现，完美平衡了性能与安全性</p>
                    <div class="flex space-x-4">
                        <a href="#features" class="bg-white text-indigo-700 px-6 py-3 rounded-lg font-medium hover:bg-gray-100 transition duration-300">
                            <i class="fas fa-book-open mr-2"></i>核心特性
                        </a>
                        <a href="#implementation" class="border-2 border-white text-white px-6 py-3 rounded-lg font-medium hover:bg-white hover:text-indigo-700 transition duration-300">
                            <i class="fas fa-code mr-2"></i>实现原理
                        </a>
                    </div>
                </div>
                <div class="md:w-1/2 mt-10 md:mt-0">
                    <div class="bg-white bg-opacity-20 p-6 rounded-xl backdrop-filter backdrop-blur-sm">
                        <div class="mermaid">
                            graph LR
                            A[ConcurrentHashMap] --> B[线程安全]
                            A --> C[高性能]
                            A --> D[弱一致性]
                            B --> B1[分段锁 JDK7]
                            B --> B2[CAS JDK8+]
                            C --> C1[读无锁]
                            C --> C2[细粒度锁]
                            D --> D1[非阻塞读]
                            D --> D2[部分可见性]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="container mx-auto px-6 md:px-12 py-16">
        <!-- Introduction -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-500 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">概述</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-8">
                <p class="text-lg mb-6"><span class="first-letter">C</span>oncurrentHashMap 是 Java 中一个用于并发编程的高效线程安全的哈希表实现。它被设计用于在多线程环境下提供比 HashMap 更加安全的操作，同时又避免了传统同步机制可能带来的性能瓶颈。</p>
                
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mt-10">
                    <div class="bg-gray-50 p-6 rounded-lg border border-gray-100">
                        <div class="flex items-center mb-4">
                            <div class="feature-icon">
                                <i class="fas fa-shield-alt text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">线程安全</h3>
                        </div>
                        <p class="text-gray-600">通过巧妙的分段锁机制和CAS操作保证多线程环境下的数据一致性</p>
                    </div>
                    <div class="bg-gray-50 p-6 rounded-lg border border-gray-100">
                        <div class="flex items-center mb-4">
                            <div class="feature-icon">
                                <i class="fas fa-bolt text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">高性能</h3>
                        </div>
                        <p class="text-gray-600">读操作完全无锁，写操作采用细粒度锁，最大限度减少线程竞争</p>
                    </div>
                    <div class="bg-gray-50 p-6 rounded-lg border border-gray-100">
                        <div class="flex items-center mb-4">
                            <div class="feature-icon">
                                <i class="fas fa-project-diagram text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">弱一致性</h3>
                        </div>
                        <p class="text-gray-600">读取线程可能会看到最新的修改或修改前的值，但不会被阻塞</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Features Section -->
        <section id="features" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-500 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">核心特性</h2>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                <!-- Feature 1 -->
                <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="text-indigo-500 mr-4">
                            <i class="fas fa-lock text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold">线程安全性</h3>
                    </div>
                    <p class="text-gray-600 mb-4">ConcurrentHashMap 是线程安全的。这意味着多个线程可以同时访问 ConcurrentHashMap，而不会引发数据不一致或线程间的冲突。不同于传统的 HashMap，ConcurrentHashMap 通过巧妙的分段锁机制（JDK 8 之前）和 CAS 操作（JDK 8 之后）来保证线程安全。</p>
                    
                    <div class="highlight-box p-4 my-4">
                        <p class="font-medium text-gray-700">关键点：</p>
                        <ul class="list-disc pl-5 mt-2 space-y-1">
                            <li>多线程安全访问，无数据竞争</li>
                            <li>JDK7: 分段锁机制</li>
                            <li>JDK8+: CAS 操作实现</li>
                        </ul>
                    </div>
                </div>

                <!-- Feature 2 -->
                <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="text-indigo-500 mr-4">
                            <i class="fas fa-tachometer-alt text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold">高效的并发性</h3>
                    </div>
                    <p class="text-gray-600 mb-4">ConcurrentHashMap 的设计初衷就是在高并发环境中提供高效的访问。它通过减少锁的粒度和使用无锁操作来尽量减少线程间的竞争。与 Collections.synchronizedMap 不同，后者通过对整个 Map 加锁来实现线程安全，ConcurrentHashMap 的设计更为精细，从而在高并发情况下具有更好的性能。</p>
                    
                    <div class="bg-gray-50 p-4 rounded-lg my-4">
                        <div class="flex items-center text-sm text-gray-500 mb-2">
                            <i class="fas fa-info-circle mr-2"></i>
                            <span>性能对比</span>
                        </div>
                        <div class="mermaid">
                            barChart
                            title 并发访问性能对比
                            x-axis 实现方式
                            y-axis 吞吐量
                            bar SynchronizedMap: 50
                            bar ConcurrentHashMap: 200
                        </div>
                    </div>
                </div>

                <!-- Feature 3 -->
                <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="text-indigo-500 mr-4">
                            <i class="fas fa-random text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold">弱一致性</h3>
                    </div>
                    <p class="text-gray-600 mb-4">ConcurrentHashMap 提供了一种弱一致性的保证，即读取操作可能会在没有获取锁的情况下获取到正在修改的数据。虽然这种设计保证了高并发情况下的性能，但也意味着读取线程可能会看到最新的修改，或者看到的是某个线程正在进行修改之前的值。换句话说，读取操作不会因为写入操作而被阻塞。</p>
                    
                    <div class="highlight-box p-4 my-4">
                        <p class="font-medium text-gray-700">一致性模型：</p>
                        <ul class="list-disc pl-5 mt-2 space-y-1">
                            <li>不强求实时一致性</li>
                            <li>读取不会被写入阻塞</li>
                            <li>适合高并发读场景</li>
                        </ul>
                    </div>
                </div>

                <!-- Feature 4 -->
                <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="text-indigo-500 mr-4">
                            <i class="fas fa-tasks text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold">批量操作</h3>
                    </div>
                    <p class="text-gray-600 mb-4">在 JDK 8 之后，ConcurrentHashMap 还引入了很多批量操作的方法，例如 forEach、reduce 和 search 等，这些方法能够在并发环境下高效处理大规模数据。批量操作的方法内部利用了 ForkJoinPool 来并行处理，这大大提高了处理效率。</p>
                    
                    <div class="code-block my-4">
                        <p class="text-sm text-gray-500 mb-2">示例代码：</p>
                        <pre>// JDK8+ 批量操作示例
map.forEach(2, (k, v) -> System.out.println(k + "=" + v));

long sum = map.reduceValues(2, Long::sum);</pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Implementation Details -->
        <section id="implementation" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-500 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">实现原理</h2>
            </div>

            <!-- JDK7 Implementation -->
            <div class="bg-white rounded-xl shadow-md p-8 mb-10 card-hover">
                <div class="flex items-center mb-6">
                    <div class="bg-indigo-100 text-indigo-700 px-3 py-1 rounded-full text-sm font-medium mr-4">
                        JDK 1.7
                    </div>
                    <h3 class="text-xl font-semibold">分段锁机制</h3>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                    <div>
                        <p class="text-gray-600 mb-4">在 JDK 7 及之前的实现中，ConcurrentHashMap 使用了分段锁（Segmented Locking）来保证并发安全：</p>
                        
                        <ul class="list-disc pl-5 space-y-2 mb-6">
                            <li><span class="font-medium">分段锁</span>：整个 ConcurrentHashMap 被分成多个 Segment，每个 Segment 类似于一个小型的哈希表，并且有一个锁与之关联。当线程访问 ConcurrentHashMap 时，只会锁住它要操作的 Segment，而不是锁住整个哈希表。</li>
                            <li><span class="font-medium">读操作无锁</span>：在大多数情况下，读取操作是无锁的。因为读取操作不会修改哈希表的结构，所以 ConcurrentHashMap 允许多个线程同时读取而不需要加锁，从而提高了性能。</li>
                        </ul>
                        
                        <div class="highlight-box p-4">
                            <p class="font-medium text-gray-700">关键特点：</p>
                            <ul class="list-disc pl-5 mt-2 space-y-1">
                                <li>底层是通过数组+链表实现</li>
                                <li>采用分段锁来实现线程安全</li>
                                <li>数据存储在Segment中，每个Segment包含HashEntry数组</li>
                            </ul>
                        </div>
                    </div>
                    
                    <div>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <img src="https://cdn.nlark.com/yuque/0/2021/png/21449790/1634605642127-4b4261d6-281c-44c6-a266-4c191aaeb015.png" alt="JDK7 ConcurrentHashMap结构" class="rounded-lg shadow-sm mb-4">
                            <p class="text-sm text-gray-500 text-center">JDK7 ConcurrentHashMap 结构示意图</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- JDK8 Implementation -->
            <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="bg-indigo-100 text-indigo-700 px-3 py-1 rounded-full text-sm font-medium mr-4">
                        JDK 1.8+
                    </div>
                    <h3 class="text-xl font-semibold">CAS 操作和分桶机制</h3>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                    <div>
                        <p class="text-gray-600 mb-4">从 JDK 8 开始，ConcurrentHashMap 摒弃了分段锁机制，转而使用更加细粒度的 CAS 操作和分桶（Bucket）机制来实现高效并发：</p>
                        
                        <ul class="list-disc pl-5 space-y-2 mb-6">
                            <li><span class="font-medium">分桶机制</span>：整个 ConcurrentHashMap 内部结构是一个包含许多 Node 的数组，每个 Node 都存储着一个键值对。插入、更新、删除等操作主要是在每个桶上进行的，因此每个桶都可以独立地进行并发操作。</li>
                            <li><span class="font-medium">CAS 操作</span>：对于插入和更新等操作，ConcurrentHashMap 使用了 CAS（Compare-And-Swap）操作来保证线程安全。</li>
                            <li><span class="font-medium">红黑树优化</span>：与 HashMap 类似，当某个桶中的链表长度超过一定阈值时，链表会被转换为红黑树，进一步提高查找和插入的效率。</li>
                        </ul>
                        
                        <div class="highlight-box p-4">
                            <p class="font-medium text-gray-700">关键特点：</p>
                            <ul class="list-disc pl-5 mt-2 space-y-1">
                                <li>底层通过数组+链表+红黑树实现</li>
                                <li>数据存储在Node数组中</li>
                                <li>多线程安全基于同步锁和CAS乐观锁</li>
                            </ul>
                        </div>
                    </div>
                    
                    <div>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <img src="https://cdn.nlark.com/yuque/0/2021/png/21449790/1634607329315-c63d8ef6-f655-46f9-b457-764966e2cf6e.png" alt="JDK8 ConcurrentHashMap结构" class="rounded-lg shadow-sm mb-4">
                            <p class="text-sm text-gray-500 text-center">JDK8 ConcurrentHashMap 结构示意图</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Use Cases -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-500 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">典型应用场景</h2>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="text-indigo-500 mr-3">
                            <i class="fas fa-database text-xl"></i>
                        </div>
                        <h3 class="text-lg font-semibold">缓存实现</h3>
                    </div>
                    <p class="text-gray-600">在需要高并发访问的缓存系统中，ConcurrentHashMap 是非常理想的选择。</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="text-indigo-500 mr-3">
                            <i class="fas fa-calculator text-xl"></i>
                        </div>
                        <h3 class="text-lg font-semibold">计数器</h3>
                    </div>
                    <p class="text-gray-600">当多个线程需要共享一个计数器时，ConcurrentHashMap 可以用来存储计数器的值，并保证线程安全。</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="text-indigo-500 mr-3">
                            <i class="fas fa-network-wired text-xl"></i>
                        </div>
                        <h3 class="text-lg font-semibold">分布式系统</h3>
                    </div>
                    <p class="text-gray-600">在需要处理大量并发请求的分布式系统中，ConcurrentHashMap 可以作为数据共享的基础设施。</p>
                </div>
            </div>
        </section>

        <!-- Version Comparison -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-indigo-500 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">版本对比</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="grid grid-cols-1 md:grid-cols-12">
                    <!-- Header -->
                    <div class="md:col-span-3 bg-gray-50 p-4 border-b md:border-b-0 md:border-r">
                        <h3 class="font-semibold text-gray-700">特性</h3>
                    </div>
                    <div class="md:col-span-5 bg-indigo-50 p-4 border-b md:border-b-0 md:border-r">
                        <h3 class="font-semibold text-indigo-700">JDK 1.7</h3>
                    </div>
                    <div class="md:col-span-4 bg-purple-50 p-4">
                        <h3 class="font-semibold text-purple-700">JDK 1.8+</h3>
                    </div>
                    
                    <!-- Row 1 -->
                    <div class="md:col-span-3 p-4 border-b md:border-b-0 md:border-r bg-gray-50">
                        <p class="font-medium">底层结构</p>
                    </div>
                    <div class="md:col-span-5 p-4 border-b md:border-b-0 md:border-r">
                        <p>数组 + 链表</p>
                    </div>
                    <div class="md:col-span-4 p-4 border-b md:border-b-0">
                        <p>数组 + 链表 + 红黑树</p>
                    </div>
                    
                    <!-- Row 2 -->
                    <div class="md:col-span-3 p-4 border-b md:border-b-0 md:border-r bg-gray-50">
                        <p class="font-medium">并发控制</p>
                    </div>
                    <div class="md:col-span-5 p-4 border-b md:border-b-0 md:border-r">
                        <p>分段锁 (Segment Lock)</p>
                    </div>
                    <div class="md:col-span-4 p-4 border-b md:border-b-0">
                        <p>CAS + synchronized</p>
                    </div>
                    
                    <!-- Row 3 -->
                    <div class="md:col-span-3 p-4 border-b md:border-b-0 md:border-r bg-gray-50">
                        <p class="font-medium">读操作</p>
                    </div>
                    <div class="md:col-span-5 p-4 border-b md:border-b-0 md:border-r">
                        <p>无锁</p>
                    </div>
                    <div class="md:col-span-4 p-4 border-b md:border-b-0">
                        <p>无锁</p>
                    </div>
                    
                    <!-- Row 4 -->
                    <div class="md:col-span-3 p-4 border-b md:border-b-0 md:border-r bg-gray-50">
                        <p class="font-medium">写操作</p>
                    </div>
                    <div class="md:col-span-5 p-4 border-b md:border-b-0 md:border-r">
                        <p>分段锁</p>
                    </div>
                    <div class="md:col-span-4 p-4 border-b md:border-b-0">
                        <p>CAS + synchronized (桶级别)</p>
                    </div>
                    
                    <!-- Row 5 -->
                    <div class="md:col-span-3 p-4 md:border-r bg-gray-50">
                        <p class="font-medium">批量操作</p>
                    </div>
                    <div class="md:col-span-5 p-4 md:border-r">
                        <p>不支持</p>
                    </div>
                    <div class="md:col-span-4 p-4">
                        <p>支持 (forEach, reduce, search)</p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto px-6 md:px-12">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索技术之美，分享编程之趣</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        <span>访问技术小馆语雀</span>
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm text-gray-500">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>